# -*- coding: utf-8 -*-
# @Time : 2021/9/10 10:55
# @Author : hello
# @Email : xxx@gmail.com
# @File : CRM_ST_KHGL_138.py
# @Project : crm

from selenium import webdriver
from time import sleep

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
"""线索模块-验证线索转换成客户功能-所有输入框合法输入"""
driver=webdriver.Chrome()
try:
    driver.implicitly_wait(30)
    driver.maximize_window()
    """前置条件-已登录crm系统且进入线索展示界面，已有线索数据"""
    cookies={'name':'PHPSESSID','value':'5o422r3lhfr2q9gmj0mmbfl2v7'}
    driver.get("http://192.168.1.155")
    driver.add_cookie(cookies)
    driver.get("http://192.168.1.155/crm/index.php?m=leads")

    clickadd = (By.CSS_SELECTOR, 'body > div.container > div.row > div:nth-child(1) > div > a')
    sleep(2)
    driver.find_element(*clickadd).click()

    fzrs = "admin"
    companys = "郑州科技有限公司"
    sources = "合作伙伴"
    lxrs = "张三"
    zws = "业务经理"
    phones = "15632455632"
    zcs = "先生"
    yxs = "xx@xx.com"
    xs = "河南省"
    ss = "郑州市"
    qs = "惠济区"
    lxtimes = "2021-09-17"
    lxnrs = "初步沟通"
    bzs = "小型软件公司"
    jds = "东大街"

    fzr = (By.ID, 'owner_name')
    company = (By.ID, 'name')
    source = (By.ID, 'source')
    lxr = (By.ID, 'contacts_name')
    zw = (By.ID, 'position')
    phone = (By.ID, 'mobile')
    zc = (By.ID, 'saltname')
    yx = (By.ID, 'email')
    adress_x = (By.NAME, "address['state']")
    adress_s = (By.NAME, "address['city']")
    adress_q = (By.NAME, "address['area']")
    jd = (By.NAME, "address['street']")
    lxtimek = (By.ID, "nextstep_time")
    lxnr = (By.ID, 'nextstep')
    bz = (By.ID, 'description')
    bc = (By.CSS_SELECTOR, 'input[value="保存"]')

    driver.find_element(*fzr).clear()
    driver.find_element(*fzr).send_keys(fzrs)
    driver.find_element(*company).send_keys(companys)
    driver.find_element(*lxr).send_keys(lxrs)
    driver.find_element(*zw).send_keys(zws)
    driver.find_element(*phone).send_keys(phones)
    driver.find_element(*yx).send_keys(yxs)
    driver.find_element(*lxtimek).send_keys(lxtimes)
    driver.find_element(*lxnr).send_keys(lxnrs)
    driver.find_element(*bz).send_keys(bzs)
    sleep(3)
    sel_locator = driver.find_element(*source)
    Select(sel_locator).select_by_visible_text(sources)
    zc_locator = driver.find_element(*zc)
    Select(zc_locator).select_by_visible_text(zcs)
    x_locator = driver.find_element(*adress_x)
    Select(x_locator).select_by_visible_text(xs)
    s_locator = driver.find_element(*adress_s)
    Select(s_locator).select_by_visible_text(ss)
    q_locator = driver.find_element(*adress_q)
    Select(q_locator).select_by_visible_text(qs)
    driver.find_element(*jd).send_keys(jds)
    driver.find_element(*bc).click()
    sleep(3)

#点击转换
    tables=(By.CSS_SELECTOR,"#form1 > table")
    tbodys=(By.TAG_NAME,"tbody")
    trs = (By.TAG_NAME, "tr")
    tds = (By.TAG_NAME, "td")
    a = (By.TAG_NAME, "a")
    table=driver.find_element(*tables)
    tbody=table.find_element(*tbodys)
    trss=tbody.find_elements(*trs)
    for tr in trss:
        td=tr.find_elements(*tds)
        if td[2]:
            sleep(3)
            td[-1].find_elements(*a)[1].click()
            break
    sleep(3)




    driver.find_element(*bc).click()

except Exception as e:
    raise e
finally:
    sleep(3)
    driver.quit()